package com.mxun.winefox.components



import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.mxun.winefox.data.VoiceModel

@Composable
fun SettingsDrawer(
    persona: String,
    onPersonaChange: (String) -> Unit,
    isTTSEnabled: Boolean,
    onTTSEnabledChange: (Boolean) -> Unit,
    isActiveChatEnabled: Boolean,
    onActiveChatEnabledChange: (Boolean) -> Unit,
    voiceModels: List<VoiceModel>,
    selectedVoiceId: String,
    onVoiceModelSelected: (String) -> Unit,
    onClearChatHistory: () -> Unit,
    onCloseDrawer: () -> Unit,
    modifier: Modifier = Modifier,
    isBilingualDisplay: Boolean,
    onBilingualDisplayChange: (Boolean) -> Unit,
    ) {
    var voiceModelExpanded by remember { mutableStateOf(false) }
    val selectedVoiceModel = voiceModels.firstOrNull { it.id == selectedVoiceId } 
        ?: voiceModels.last()

    Column(
        modifier = modifier
            .fillMaxHeight()
            .width(300.dp)
            .background(MaterialTheme.colorScheme.surface)
            .padding(16.dp),
        verticalArrangement = Arrangement.SpaceBetween
    ) {
        Column {
            DrawerHeader(onCloseDrawer)
            PersonaSetting(persona, onPersonaChange)
            VoiceModelSetting(
                voiceModels,
                selectedVoiceModel,
                voiceModelExpanded,
                onVoiceModelSelected,
                onExpandedChange = { voiceModelExpanded = it }
            )
            FeatureSwitches(
                isTTSEnabled,
                onTTSEnabledChange,
                isActiveChatEnabled,
                onActiveChatEnabledChange,
                isBilingualDisplay,
                onBilingualDisplayChange
            )
        }
        ClearChatButton(onClearChatHistory)
    }
}

@Composable
private fun DrawerHeader(onCloseDrawer: () -> Unit) {
    Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text("设置菜单", style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Bold)
        IconButton(onClick = onCloseDrawer) {
            Icon(Icons.Default.Close, contentDescription = "关闭设置")
        }
    }
    Spacer(modifier = Modifier.height(24.dp))
}

@Composable
private fun PersonaSetting(persona: String, onPersonaChange: (String) -> Unit) {
    Text("人设", style = MaterialTheme.typography.titleMedium)
    Spacer(modifier = Modifier.height(8.dp))
    OutlinedTextField(
        value = persona,
        onValueChange = onPersonaChange,
        modifier = Modifier.fillMaxWidth(),
        maxLines = 3
    )
    Spacer(modifier = Modifier.height(24.dp))
}

@Composable
private fun VoiceModelSetting(
    voiceModels: List<VoiceModel>,
    selectedModel: VoiceModel,
    expanded: Boolean,
    onModelSelected: (String) -> Unit,
    onExpandedChange: (Boolean) -> Unit
) {
    Text("语音模型", style = MaterialTheme.typography.titleMedium)
    Spacer(modifier = Modifier.height(8.dp))
    
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .clip(RoundedCornerShape(4.dp))
            .background(MaterialTheme.colorScheme.surfaceVariant)
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .clickable { onExpandedChange(true) }
                .padding(horizontal = 16.dp, vertical = 12.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Text(selectedModel.name, color = MaterialTheme.colorScheme.onSurface)
            Icon(
                Icons.Default.ArrowDropDown,
                contentDescription = "选择语音模型",
                tint = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f)
            )
        }

        DropdownMenu(
            expanded = expanded,
            onDismissRequest = { onExpandedChange(false) },
            modifier = Modifier
                .fillMaxWidth(0.9f)
                .background(MaterialTheme.colorScheme.surface)
        ) {
            voiceModels.forEach { model ->
                DropdownMenuItem(
                    text = { Text(model.name, modifier = Modifier.fillMaxWidth()) },
                    onClick = {
                        onModelSelected(model.id)
                        onExpandedChange(false)
                    }
                )
            }
        }
    }
    Spacer(modifier = Modifier.height(24.dp))
}

@Composable
fun FeatureSwitches(
    isTTSEnabled: Boolean,
    onTTSEnabledChange: (Boolean) -> Unit,
    isActiveChatEnabled: Boolean,
    onActiveChatEnabledChange: (Boolean) -> Unit,
    isBilingualDisplay: Boolean,
    onBilingualDisplayChange: (Boolean) -> Unit
) {
    Text("功能设置", style = MaterialTheme.typography.titleMedium)
    Spacer(modifier = Modifier.height(8.dp))

    SwitchRow("语音播报", isTTSEnabled, onTTSEnabledChange)
    SwitchRow("主动聊天", isActiveChatEnabled, onActiveChatEnabledChange)
    SwitchRow("中日双语显示", isBilingualDisplay, onBilingualDisplayChange)
}


@Composable
private fun SwitchRow(label: String, checked: Boolean, onCheckedChange: (Boolean) -> Unit) {
    Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(label)
        Switch(checked = checked, onCheckedChange = onCheckedChange)
    }
}

@Composable
private fun ClearChatButton(onClick: () -> Unit) {
    Button(
        onClick = onClick,
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 16.dp),
        colors = ButtonDefaults.buttonColors(
            containerColor = MaterialTheme.colorScheme.errorContainer,
            contentColor = MaterialTheme.colorScheme.onErrorContainer
        )
    ) {
        Icon(Icons.Default.Delete, contentDescription = "清除聊天记录")
        Spacer(modifier = Modifier.width(8.dp))
        Text("清除聊天记录")
    }
}